<?php
namespace Admin\Model;
use Common\Model\CommonModel;
//绘画作品
class RoomModel extends  CommonModel{
	
	//自动验证
	protected $_validate = array(
			//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
			array('floor', 'require', '楼层不能为空！', 1, 'regex', 3),
			array('building', 'require', '楼号不能为空！', 1, 'regex', 3),
			array('name', 'require', '房间号不能为空！', 1, 'regex', 3),
			array('maxnum', 'require', '最大可住人数不能为空！', 1, 'regex', 3),
			array('maxnum', 'number', '最大可住人数格式错误！', 1, 'regex', 3),
		array('changenum', 'require', '状态改变人数不能为空！', 1, 'regex', 3),
		array('changenum', 'number', '状态改变人数格式错误！', 1, 'regex', 3),

			
	);
	

	public function _after_update() {
		return true;
	}

	public function _after_insert() {
		parent::_after_insert();
		$this->data_cache();
	}
	//删除操作时删除缓存
	public function _after_delete() {
		parent::_after_delete();
		$this->data_cache();
	}


	public  function get_rooms_select(){


		$data=$this->field("id,name,building,status,type,isru")->select();


		foreach($data as $k=>$v){

			$result[$v["building"]]["name"]=F("Var_building")[$v["building"]]["name"];
			$result[$v["building"]]["child"][]=array(

				"id"=>$v["id"],
				"name"=>$v["name"],
				"type"=>F("Var_room_type")[$v["type"]]["name"],
				"status"=>$v["status"],
				"isru"=>$v["isru"]
			);








		}

		return $result;



	}
	
	public function data_cache(){
		

		$d=$this->field("status",true)->select();

		$s=array();
		foreach ($d as $key=>$t){

			$s[$t[id]]=$t;

			$all[$t[id]]=$t[name];
		}


		F("Room",$s);

		$building=F("Var_building");

		foreach($building as $k=>$v){

			$data[$k]=$this->where(array("building"=>$k))->group("floor")->getField("floor",true);

			F("Building",$data);

		}






	}


	public function update_status($room_id){




		$num=D("RoomLog")->where(array("room_id"=>$room_id,"status"=>0))->count();

		$room_changenum=F("Room")[$room_id][changenum];
		$room_maxnum=F("Room")[$room_id][maxnum];


		$v["id"]=$room_id;
		if($num>=$room_changenum){

			$v["status"]=0;

		}
		elseif($num==0){

			$v["status"]=1;
		}
		else{

			$v["status"]=2;
		}


		if($num<$room_maxnum){

			$v["isru"]=1;

		}

		else{

			$v["isru"]=0;
		}




		$this->save($v);

		return $v["status"];



	}


	
}